//
//  SSHomeHeaderView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/7.
//

import UIKit

import SnapKit
import JXBanner

@objc public protocol SSHomeHeaderViewDelegate : AnyObject {
    
    @objc optional func didSelectItem(index: Int)
    @objc optional func didADSelectItem(index: Int)

}


class SSHomeHeaderView: UIView {
    
    @IBOutlet weak var bannerView: PageView!
 
    @IBOutlet weak var serviceBanner: UIImageView!
    @IBOutlet weak var bannerView2: PageView!
    public var bannerDatas: Array<Any> = []
    public var bannerDatas1: Array<Any> = []
    public weak var delegate: SSHomeHeaderViewDelegate?
    
    class func homeHeaderView()->SSHomeHeaderView{
        return Bundle.main.loadNibNamed("SSHomeHeaderView", owner: nil, options: nil)?.last as! SSHomeHeaderView
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    func setupUI() {
        
        bannerView.tag = 10
        bannerView.dataSource = self
        bannerView.delegate = self
        bannerView.isInfinite = false
        bannerView.registerCell(PageCustomCell.self)
        bannerView.interitemSpacing = 16
        let size = CGSize(width: UIScreen.main.bounds.size.width - 20, height: 100)
        let transform = CGAffineTransform(scaleX: 0.9, y: 1)
        bannerView.itemSize = size.applying(transform)
        bannerView.transformer?.type = .linear
        bannerView.decelerationDistance = PageView.automaticDistance
        
        bannerView2.tag = 20
        bannerView2.dataSource = self
        bannerView2.delegate = self
        bannerView2.isInfinite = false
        bannerView2.registerCell(PageCustomCell.self)
        bannerView2.interitemSpacing = 16
        let size2 = CGSize(width: UIScreen.main.bounds.size.width - 20, height: 155)
        let transform2 = CGAffineTransform(scaleX: 0.9, y: 1)
        bannerView2.itemSize = size2.applying(transform2)
        bannerView2.transformer?.type = .linear
        bannerView2.decelerationDistance = PageView.automaticDistance
        
        let m1odel = SSBanner()
        m1odel.bgName = "normal_clean"
        m1odel.isLocationImg = true

        let m1odel1 = SSBanner()
        m1odel1.bgName = "checkout_clean"
        m1odel1.isLocationImg = true

        let m1odel2 = SSBanner()
        m1odel2.bgName = "deep_clean"
        m1odel2.isLocationImg = true

        let m1odel3 = SSBanner()
        m1odel3.bgName = "steam_carpet_clean"
        m1odel3.isLocationImg = true
        
        bannerDatas1 = [m1odel,m1odel1,m1odel2,m1odel3]
        
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel))
        serviceBanner.addGestureRecognizer(tap)
        serviceBanner.isUserInteractionEnabled = true
        
    }

    
    @objc private func tapedTitleLabel() {
//        UIViewController.current().present(SSQRCodeViewController(), animated: true)
        enterMeiqia()
    }
    
    @IBAction func cleanClick(_ sender: Any) {
        let btn: UIButton = sender as! UIButton
        delegate?.didSelectItem?(index: btn.tag)
        if (btn.tag == 1) {
            print("1")
        }else if (btn.tag == 2){
            print("2")
        }else if (btn.tag == 3){
            print("3")
        }else if (btn.tag == 4){
            print("4")
        }
        
    }
    
   public func setbanners(banners:Array<SSBanner>) {
        self.bannerDatas = banners
        self.bannerView.reloadData()
    }
    
}

extension SSHomeHeaderView:  PageViewDataSource, PageViewDelegate {
    
    func numberOfItems(in pageView: PageView) -> Int {
        if(pageView.tag == 10){
            return bannerDatas.count
        }else{
            return bannerDatas1.count
        }
    }

    func pageView(_ pageView: PageView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = pageView.dequeueReusableCell(PageCustomCell.self, indexPath: indexPath)
        if(pageView.tag == 10){
            cell.routeModel = bannerDatas[indexPath.row] as? SSBanner
        }else{
            cell.routeModel = bannerDatas1[indexPath.row] as? SSBanner
        }
        return cell
    }
    
    func pageView(_ pageView: PageView, didSelectItemAt index: Int) {
        debugPrint("\(index)")
        if pageView == bannerView  {
            delegate?.didADSelectItem?(index: index + 1)
        }else{
            delegate?.didSelectItem?(index: index + 1)
        }
        
        
    }
}


func enterMeiqia() {
       let chatViewManager = MQChatViewManager.init()
       chatViewManager.setoutgoingDefaultAvatarImage(UIImage(named: "AppIcon"))
    chatViewManager.pushMQChatViewController(in: UIViewController.current())
   }

